/*
 ***************************************************************************
 * MediaTeK Inc.
 * 4F, No. 2 Technology	5th	Rd.
 * Science-based Industrial	Park
 * Hsin-chu, Taiwan, R.O.C.
 *
 * (c) Copyright 2002-2012, MTK.
 *
 * All rights reserved.	MediaTeK's source	code is	an unpublished work	and	the
 * use of a	copyright notice does not imply	otherwise. This	source code
 * contains	confidential trade secret material of MediaTeK Tech. Any attemp
 * or participation	in deciphering,	decoding, reverse engineering or in	any
 * way altering	the	source code	is stricitly prohibited, unless	the	prior
 * written consent of MediaTeK, Inc. is obtained.
 ***************************************************************************

	Module Name:
	omci.h

	Abstract:

	Revision History:
	Who			When			What
	--------	----------		----------------------------------------------
	Name		Date			Modification logs
	jq.zhu		2012/6/28	Create
*/
#ifndef _OMCI_H_
#define _OMCI_H_


#include <semaphore.h>
#include <pthread.h>
#include <sched.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <fcntl.h>

#include "omci_types.h"
#include "omci_me.h"
#include "omci_timer.h"
#include "omci_external_msg.h"
#include "omci_message_handle.h"
#include "omci_lib_internal.h"
#include "omci_internal_export.h"
#include "omci_check_memory_leak.h"


/************************************************************************
*                        			MACRO							
************************************************************************
*/
#ifndef GNU_PACKED 
#define GNU_PACKED  __attribute__ ((packed))
#endif // GNU_PACKED //

#define MODULE_VERSION_OMCI	("1.0")
#define OMCI_GET_INSTANC_BY_ATTR_MAX_NUM  10
#define MAX_OMCI_PKT_LEN (1980)
#define MAX_OMCI_SOFTIMAGE_UPDATE_WINDOW (256)

typedef enum {
	ATTRIBUTE_TABLE_NULL = 0,
	ATTRIBUTE_TABLE_REPLACE_ENTRY,
	ATTRIBUTE_TABLE_ADD_ENTRY,
	ATTRIBUTE_TABLE_DEL_ENTRY,
	ATTRIBUTE_TABLE_UPDATE_ENTRY
} ATRRIBUT_TABLE_ACTION ;

typedef enum {
	OMCI_STATE_START = 0,
	OMCI_STATE_IDLE,
	OMCI_STATE_INIT,
	OMCI_STATE_RUN,
} OMCI_STATE_t ;

/************************************************************************
*                        			struct							
************************************************************************
*/
typedef struct omci_s
{
	uint16_t version;
	int meTableSize; /*the value is calculated at initilized stage.*/
	int stdMeTblSize; /*standard(define in 988) ME size*/
	int vendorMeTblSize; /*private ME size*/
	omciCapablity_t omciCap;
	uint8_t omciDataSync; //call omciDataSyncSeqIncret to increase it
	uint8_t hadInitInst; //do not mib reset twice
	uint8_t omciAlarmSeq; //   should to 1~255	; when OMCI reset, the omciAlarmSeq=0
	uint8_t onuAdminState; //ONU-G administrator state
	uint16_t onuId;
	OMCI_STATE_t omciState;
}omci_t,*omci_ptr;

typedef struct tableAttriNode_s{
	struct tableAttriNode_s *next;
	uint16_t classId;
	uint16_t instId;
	uint16_t seqNum;//from 0 on
	char attributeName[ATTRIBUTE_NAME_LENGTH];
	uint32_t totalLength;
	uint32_t tableLength;
	uint8_t *tableContant;
	int tmrId;
}tableAttriNode_t , *tableAttriNode_ptr;

typedef struct tableAttri_s{
	pthread_mutex_t tableAttriLock;
	tableAttriNode_t *tableList;
}tableAttri_t, *tableAttri_ptr;

typedef struct eventThreadPara_s{
	omci_evt_id_t eventId;
	union{
		evt_avc_data_t evtAvcData;
		evt_alarm_data_t evtAlmData;
		evt_test_result_data_t evtTestData;
		evt_arc_data_t evtArcData;
		omci_me_info_t evtAvcRepRateData;
	};
}eventThreadPara_t;

typedef struct event_queue_msg_s{
	long msgType;
	eventThreadPara_t data;
}event_queue_msg_t;

typedef struct {
    struct{
        unsigned char msg[MAX_OMCI_PKT_LEN]; /*omci pkt body*/
    }buf[MAX_OMCI_SOFTIMAGE_UPDATE_WINDOW];  /*buf to hold omci pkts*/

    sem_t sem;              /*inter-thread sync semaphore*/
    unsigned short r_idx;   /*read index*/
    unsigned short w_idx;   /*write index*/
} omciRxBuf_t;

OUT int omciPktSend( IN omciGenericPayload_t *omciPayLoad, IN uint8_t msgDst );
OUT omciMeInst_t *omciGetInstanceByMeId(IN omciManageEntity_ptr me_p ,  IN uint16_t meId);
omciManageEntity_t *omciGetMeByClassId(uint16_t classId);
uint8_t omciGetAttriNumOfMe(omciManageEntity_t *me_p);
OUT uint8_t *omciGetInstAttriByName(IN omciMeInst_t *inst_p , IN char *attriName, OUT uint16_t *length);
OUT uint8_t *omciGetInstAttriByMask(IN omciMeInst_t *inst_p , IN uint16_t attriMask, OUT uint16_t *length);
int omciAddTableValueToInst(IN omciMeInst_t *inst_p , IN omciTableAttriValue_t *table_ptr, IN omciAttriDescript_ptr omciAttribute, IN uint8_t *value, IN uint16_t sameEntryNum, IN uint8_t action);
OUT int omciDelTableValueOfInst(IN omciMeInst_t *inst_p , IN uint8_t attriIndex);
OUT omciTableAttriValue_t *omciGetTableValueByIndex(IN omciMeInst_t *inst_p , IN uint8_t attriIndex);
OUT int omciAddTableContent(IN uint16_t meClassId , IN uint16_t instId, IN uint8_t *table_ptr, IN uint32_t tableLength ,IN char *attributeName);
OUT uint32_t omciGetTableContent(IN uint16_t meClass, IN uint16_t instId, IN char *attributeName, IN uint32_t length , IN uint8_t devId, OUT uint8_t *outBuf, OUT uint16_t *seqNum);
omciMeInst_t *omciAllocInstance(omciManageEntity_t *me);
int omciFreeInstance(omciMeInst_t *inst);
int omciDelInstance(omciManageEntity_ptr me , omciMeInst_t *inst_p);
int omciAddInstanceToTail(omciManageEntity_ptr me, omciMeInst_t *newInst);
int omciRxHandle(omciGenericPayload_t * omciPkt);
int omciAlarmTcaMsgHandle(evt_alarm_data_t *alarmInfo, omci_evt_id_t event_id);
int omciAlarmTcaMsgReport_unlock(evt_alarm_data_t *alarmInfo, omci_evt_id_t event_id, omciMeInst_t *inst_p);
int omciAvcMsgHandle(evt_avc_data_t *avcInfo);
int omciTestRstMsgHandle(evt_test_result_data_t *testRstInfo);
int omciSetAlarmReportControl(IN evt_arc_data_ptr arc_info);
int omciSetAvcReportRate(omci_me_info_ptr me_info);
int omci_send_event_msg(omci_evt_id_t event_id, void *event_val);
void omci_event_process_task(void);
void omciSetRDKBShareMem(void);
int omciInitExtendOmciRxBuf(void);


extern int(*omci_pkt_handler)(omci_me_info_ptr me_info);
extern int(*omci_update_pm_history_data)();
extern int(*omci_get_softimage_version)(char *version,uint8_t ver_len, uint8_t image_flag);
extern int(*omci_get_bootflag)(uint8_t* bootflag);
extern int(*omci_set_bootflag)(uint8_t bootflag);
extern int(*omci_read_softimage_state)(softimage_state_t *image_state);
extern int(*omci_save_softimage_state)(softimage_state_t *image_state);
extern int(*omci_write_flash)(uint8_t image_flag, char*image_path);
extern int(*omci_get_current_image_version)(char *version,uint8_t ver_len, char *image_path);
extern int(*omci_is_current_image_valid)(char *image_path,uint32_t olt_crc_val);
extern int(*omci_enhanced_security_ctrl_set_msk)(uint8_t *msk, uint8_t msk_len);
extern int(*omci_get_random_data)(uint8_t *random_val);
extern int(*omci_table_mem_free)(omci_me_info_ptr me_info);
extern int(*omci_screen_print)(char *content);

extern omci_t omci;
extern omciSystemCapablity_ptr pSystemCap;
extern omciOnuCapablity_ptr	pOnuCap;
extern omciAniCapablity_ptr	pAniCap;



#endif // _OMCI_H_
